package com.xfhy.wallpaperdemo.lock;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import com.xfhy.wallpaperdemo.DaemonLog;
import com.xfhy.wallpaperdemo.MyApplication;
import com.xfhy.wallpaperdemo.R;

import java.io.PrintStream;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class NotifyUtil {
    public static final String PERMANENT_CHANNEL_ID = "wbclean_channel_id";
    public static final int PERMANENT_ID = 102;
    public static final String UPUSH_CHANNEL_ID = "upush_default";
    public static final String a = "NotifyUtil";
    public static String b = "通知";
    public static final int c = 4;
    public static final int d = 1;
    public static final int e = 2;
    public static final int f = 3;
    public static final int g = 4;
    public static final int h = 5;
    public static final int i = 6;
    public static final int j = 7;
    public static final String k = "openflashlight";
    public static final String l = "androidmodel";
    public static NotificationCompat.Builder m = null;
    public static NotificationManagerCompat n = null;
    public static String o = null;
    public static boolean p = false;
    public static RemoteViews q;
    public static Notification r;

    public static Notification getNormalPermanentNotifi(String str, String str2) {
        Log.d(a, "getNormalPermanentNotifi");
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return null;
        }
        n = NotificationManagerCompat.from(MyApplication.sContext);
        NotificationCompat.Builder a2 = a();
        m = a2;
        r = getNormalNotification(a2, str, str2);
        NotificationManagerCompat.from(MyApplication.sContext).notify(111,
                a().setContentTitle("").setContentText("").setSmallIcon(R.drawable.icon_translucent).setAutoCancel(true).setOngoing(true).build());
        NotificationManagerCompat.from(MyApplication.sContext).cancel(111);
        return r;
    }

    @NonNull
    public static RemoteViews getRemoteViews() {
        q = new RemoteViews(MyApplication.sContext.getPackageName(), R.layout.notification_view_white_nohongbao);
        return q;
    }

    @SuppressLint("WrongConstant")
    public static Notification getPermanentNotification() {
        Log.d(a, "getPermanentNotification");
        n = NotificationManagerCompat.from(MyApplication.sContext);
        RemoteViews remoteViews = getRemoteViews();
        Intent intent4 = new Intent(MyApplication.sContext, JumpActivity.class);
        intent4.setFlags(268435456);
        intent4.putExtra("key", "clean");
        //remoteViews.setOnClickPendingIntent(R.id.ll_home_clean, PendingIntent.getActivity(MyApplication.sContext, 7, intent4, 134217728));
        Intent intent5 = new Intent(MyApplication.sContext, NotifyService.class);
        if (!p) {
            intent5.setAction(NotifyService.ACTION_OPEN_FLASHLIGHT);
        } else {
            intent5.setAction(NotifyService.ACTION_CLOSE_FLASHLIGHT);
        }
        p = !p;
        //remoteViews.setOnClickPendingIntent(R.id.ll_flashlight, PendingIntent.getService(MyApplication.sContext, 5, intent5, 134217728));
        Intent intent6 = new Intent(MyApplication.sContext, JumpActivity.class);
        intent6.addFlags(268435456);
        //intent6.putExtra("key", OnekeyField.NORMAL_AREA);
        //remoteViews.setOnClickPendingIntent(R.id.ll_home, PendingIntent.getActivity(MyApplication.sContext, 3, intent6, 134217728));
        Intent intent7 = new Intent(MyApplication.sContext, JumpActivity.class);
        intent7.setFlags(268435456);
        //intent7.putExtra("key", OnekeyField.KILLBACKGROUND);
        //remoteViews.setOnClickPendingIntent(R.id.ll_head, PendingIntent.getActivity(MyApplication.sContext, 1, intent7, 134217728));
        NotificationCompat.Builder a2 = a();
        m = a2;
        r = getNotification(a2, remoteViews);
        NotificationManagerCompat.from(MyApplication.sContext).notify(111,
                a().setContentTitle("").setContentText("").setSmallIcon(R.drawable.icon_translucent).setAutoCancel(true).setOngoing(true).build());
        NotificationManagerCompat.from(MyApplication.sContext).cancel(111);
        return r;
    }

    @SuppressLint("WrongConstant")
    public static Notification getNotification(NotificationCompat.Builder builder, RemoteViews remoteViews) {
        if (Build.VERSION.SDK_INT >= 21) {
            builder.setVisibility(-1);
        }
        Intent intent = new Intent(MyApplication.sContext, JumpActivity.class);
        intent.setFlags(268435456);
        intent.putExtra("key", "other");
        return builder.setSmallIcon(R.drawable.icon_translucent).setCustomContentView(remoteViews)
                .setAutoCancel(false)
                .setShowWhen(false)
                .setContentIntent(PendingIntent.getActivity(MyApplication.sContext, 0, intent, 134217728))
                .setPriority(2).setOngoing(true).setLocalOnly(true).build();
    }

    @SuppressLint("WrongConstant")
    public static Notification getNormalNotification(NotificationCompat.Builder builder, String str, String str2) {
        if (Build.VERSION.SDK_INT >= 21) {
            builder.setVisibility(-1);
        }
        Intent intent = new Intent(MyApplication.sContext, JumpActivity.class);
        intent.setFlags(268435456);
        intent.putExtra("key", "other");
        return builder.setSmallIcon(R.drawable.icon_translucent).setContentTitle(str).setContentText(str2).setAutoCancel(false).setShowWhen(false).setContentIntent(PendingIntent.getActivity(MyApplication.sContext, 0, intent, 134217728)).setPriority(2).setOngoing(true).setLocalOnly(true).build();
    }

    @RequiresApi(api = 26)
    public static String getPermanentChannelId() {
        String str = MyApplication.sContext.getResources().getString(R.string.app_name) + "通知";
        b = str;
        @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(PERMANENT_CHANNEL_ID, str, 4);
        notificationChannel.setSound((Uri) null, (AudioAttributes) null);
        n.createNotificationChannel(notificationChannel);
        return notificationChannel.getId();
    }

    public static NotificationCompat.Builder a() {
        if (Build.VERSION.SDK_INT >= 26) {
            return new NotificationCompat.Builder(MyApplication.sContext, getPermanentChannelId());
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MyApplication.sContext, "");
        return builder;
    }

    public static void showPermanentNotification() {
        Log.d(a, "showPermanentNotification");
        if (r != null) {
            DaemonLog.d("展示通知");
            NotificationManagerCompat.from(MyApplication.sContext).notify(102, r);
        }
    }

}
